package DbConnection;


//import java.awt.HeadlessException;
import javax.swing.JOptionPane;
import java.sql.*;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Home
 */
public class login extends javax.swing.JFrame {

    /**
     * Creates new form login
     */
    private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
	private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:MKYONG";
	private static final String DB_USER = "user";
	private static final String DB_PASSWORD = "password";

    
    	private static Connection getDBConnection() {
 
		Connection dbConnection = null;
 
		try {
 
			Class.forName(DB_DRIVER);
 
		} catch (ClassNotFoundException e) {
 
			System.out.println(e.getMessage());
 
		}
 
		try {
 
			dbConnection = DriverManager.getConnection(
                             DB_CONNECTION, DB_USER,DB_PASSWORD);
			return dbConnection;
 
		} catch (SQLException e) {
 
			System.out.println(e.getMessage());
 
		}
 
		return dbConnection;
 
	}

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel_wrapper = new javax.swing.JPanel();
        jL_username = new javax.swing.JLabel();
        jTF_username = new javax.swing.JTextField();
        jL_password = new javax.swing.JLabel();
        jPasswordField = new javax.swing.JPasswordField();
        jB_submit = new javax.swing.JButton();
        jB_help = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jL_username.setText("Username");

        jL_password.setText("Password");

        jPasswordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordFieldActionPerformed(evt);
            }
        });

        jB_submit.setText("Submit");
        jB_submit.setToolTipText("");
        jB_submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ActionHandlers(evt);
            }
        });

        jB_help.setText("Help");
        jB_help.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_helpActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel_wrapperLayout = new javax.swing.GroupLayout(jPanel_wrapper);
        jPanel_wrapper.setLayout(jPanel_wrapperLayout);
        jPanel_wrapperLayout.setHorizontalGroup(
            jPanel_wrapperLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_wrapperLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel_wrapperLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jL_username)
                    .addComponent(jL_password))
                .addGap(41, 41, 41)
                .addGroup(jPanel_wrapperLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel_wrapperLayout.createSequentialGroup()
                        .addComponent(jB_submit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jB_help))
                    .addComponent(jTF_username)
                    .addComponent(jPasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE))
                .addContainerGap(128, Short.MAX_VALUE))
        );
        jPanel_wrapperLayout.setVerticalGroup(
            jPanel_wrapperLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_wrapperLayout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addGroup(jPanel_wrapperLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jL_username)
                    .addComponent(jTF_username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addGroup(jPanel_wrapperLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jL_password)
                    .addComponent(jPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(jPanel_wrapperLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jB_submit)
                    .addComponent(jB_help))
                .addContainerGap(107, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel_wrapper, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel_wrapper, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ActionHandlers(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ActionHandlers
        // TODO add your handling code here:

                 //dbConnection = null;
		//PreparedStatement preparedStatement = null;
 
		String selectSQL =  "SELECT username, password FROM user WHERE username= ?  AND password = ?";
 
		try {
                       // String username = jTF_username.getText().trim();
                       // String password = jPasswordField.getText().trim();
                        
			Connection dbConnection = getDBConnection();
			PreparedStatement preparedStatement = dbConnection.prepareStatement(selectSQL);
			preparedStatement.setInt(1, 1001);
 
			// execute select SQL stetement
			ResultSet rs = preparedStatement.executeQuery();
                        String username = rs.getString(jTF_username.getText().trim());
                        String password = rs.getString( jPasswordField.getText().trim());
                        
                        int count = 0;
			while (rs.next()) {
       
                            count = count + 1;
          
			}
                        
            if (username.length() == 0 || password.length() == 0) {
                JOptionPane.showMessageDialog(null, "Fill the text field");
            } else {
            if (count == 1) {
                    JOptionPane.showMessageDialog(null, "Success! hallo & Welkom", "Access Granted", JOptionPane.INFORMATION_MESSAGE);
            } else if (count > 1) {

                    JOptionPane.showMessageDialog(null, "Duplicated user, Access denied", "Error Message", JOptionPane.ERROR_MESSAGE);

            } else {
                    JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Error Message", JOptionPane.ERROR_MESSAGE);
                }
            }
 
            } catch (SQLException e) {
 
			System.out.println(e.getMessage());
 
            } 
		
    }                               
//GEN-LAST:event_ActionHandlers
    private void jB_helpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_helpActionPerformed
        // TODO add your handling code here:
        JOptionPane.showMessageDialog(null, "Get one good account from your Boss", "INFORMATION", JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jB_helpActionPerformed

    private void jPasswordFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jPasswordFieldActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new login().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jB_help;
    private javax.swing.JButton jB_submit;
    private javax.swing.JLabel jL_password;
    private javax.swing.JLabel jL_username;
    private javax.swing.JPanel jPanel_wrapper;
    private javax.swing.JPasswordField jPasswordField;
    private javax.swing.JTextField jTF_username;
    // End of variables declaration//GEN-END:variables
}
